﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using WFNetLib;

namespace Txt2Jar
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int length;
        private void Form1_Load(object sender, EventArgs e)
        {
            
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox4.Text = openFileDialog1.FileName;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = folderBrowserDialog1.SelectedPath;
            }
        }       

        private void button6_Click(object sender, EventArgs e)
        {
            if (openFileDialog2.ShowDialog() == DialogResult.OK)
            {
                textBox6.Text = openFileDialog2.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            using (ZipOutputStream bookstream = new ZipOutputStream(File.Create(textBox5.Text+".jar")))
            {
                bookstream.SetLevel(5);
                //生成头部信息说明文档
                CreatMANIFEST(bookstream);
                //将需要的class文件复制过来
                CreatClasses(bookstream);
                //生产数据文件
                CreatDataContent(bookstream);
                //创建小说，索引 最重要的难点
                CreatIndex(bookstream);
                bookstream.Finish();
                bookstream.Close();
            }
        }
        /// <summary>
        /// 生成小说的索引文件
        /// </summary>
        /// <param name="bookstream"></param>
        private void CreatIndex(ZipOutputStream bookstream)
        {
            List<byte> list = new List<byte>();
            list.Add(0x00);
            list.Add(0x01);
            list.Add(0x30);
            string str = "";
            ushort count = 0;
            int num2 = 0;
            byte[] buffer;
            using (MemoryStream stream = new MemoryStream())
            {
                //书名头部信息的写入
                stream.Write(list.ToArray(), 0, 3);
                str = "书名：" + textBox5.Text;
                buffer = Encoding.UTF8.GetBytes(str);
                count = (ushort)buffer.Length;
                stream.WriteByte(BytesOP.GetHighByte(count));
                stream.WriteByte(BytesOP.GetLowByte(count));
                stream.Write(buffer, 0, buffer.Length);
                //书本的内容数量
                str = "1";
                count = (ushort)str.Length;
                stream.WriteByte(BytesOP.GetHighByte(count));
                stream.WriteByte(BytesOP.GetLowByte(count));
                stream.Write(Encoding.Default.GetBytes(str), 0, count);
                /*书本中章节数量 写入完毕*/
                //章节标题信息写入
                //FileStream file = File.OpenRead(textBox4.Text);
                str = "1"+"," + length.ToString()+"," + textBox5.Text;
                buffer = Encoding.UTF8.GetBytes(str);
                count = (ushort)buffer.Length;
                stream.WriteByte(BytesOP.GetHighByte(count));
                stream.WriteByte(BytesOP.GetLowByte(count));
                stream.Write(buffer, 0, buffer.Length);
                str = "书名：" + textBox5.Text;
                buffer = Encoding.UTF8.GetBytes(str);
                count = (ushort)buffer.Length;
                stream.WriteByte(0);
                stream.WriteByte(0);
                stream.WriteByte(BytesOP.GetHighByte(count));
                stream.WriteByte(BytesOP.GetLowByte(count));
                stream.Write(buffer, 0, buffer.Length);
                str = "作者：" + textBox2.Text;
                buffer = Encoding.UTF8.GetBytes(str);
                count = (ushort)buffer.Length;
                stream.WriteByte(BytesOP.GetHighByte(count));
                stream.WriteByte(BytesOP.GetLowByte(count));
                stream.Write(buffer, 0, buffer.Length);
                str = "制作：" + textBox3.Text;
                buffer = Encoding.UTF8.GetBytes(str);
                count = (ushort)buffer.Length;
                stream.WriteByte(BytesOP.GetHighByte(count));
                stream.WriteByte(BytesOP.GetLowByte(count));
                stream.Write(buffer, 0, buffer.Length);
                ZipEntry entry = new ZipEntry("0");
                entry.Size = stream.Length;
                bookstream.PutNextEntry(entry);
                bookstream.Write(stream.ToArray(), 0, (int)stream.Length);
                bookstream.Flush();
                bookstream.CloseEntry();
            }
        }
        /// <summary>
        /// 生成小说对应章节的数据文件
        /// </summary>
        /// <param name="bookstream"></param>
        private void CreatDataContent(ZipOutputStream bookstream)
        {
            string s = File.ReadAllText(textBox4.Text,Encoding.GetEncoding("gb2312"));
            ZipEntry entry = new ZipEntry("1");
            List<byte> tt = new List<byte>();
            byte[] buffer = Encoding.Unicode.GetBytes(s);
            tt.AddRange(buffer);
            tt.Insert(0, 0xfe);
            tt.Insert(0, 0xff);
            entry.Size = tt.Count;
            length = tt.Count;
            bookstream.PutNextEntry(entry);
            bookstream.Write(tt.ToArray(), 0, tt.Count);
            bookstream.Flush();
            bookstream.CloseEntry();
        }
        /// <summary>
        /// 将需要的class文件和图片加载进来
        /// </summary>
        /// <param name="bookstream"></param>
        private void CreatClasses(ZipOutputStream bookstream)
        {
            FileStream file = null;
            ZipEntry entry = null;
            byte[] buffer;
            foreach (string s in Directory.GetFiles(Environment.CurrentDirectory + @"\Data\"))
            {
                file = File.OpenRead(s);
                buffer = new byte[file.Length];
                file.Read(buffer, 0, buffer.Length);
                entry = new ZipEntry(Path.GetFileName(s));
                entry.Size = file.Length;
                file.Close();
                bookstream.PutNextEntry(entry);
                bookstream.Write(buffer, 0, buffer.Length);
                bookstream.Flush();
                bookstream.CloseEntry();
            }
            file = File.OpenRead(textBox6.Text);
            buffer = new byte[file.Length];
            file.Read(buffer, 0, buffer.Length);
            entry = new ZipEntry("0.png");
            entry.Size = file.Length;
            file.Close();
            bookstream.PutNextEntry(entry);
            bookstream.Write(buffer, 0, buffer.Length);
            bookstream.Flush();
            bookstream.CloseEntry();
        }
        /// <summary>
        /// 生成头部信息文档
        /// </summary>
        /// <param name="bookstream"></param>
        private void CreatMANIFEST(ZipOutputStream bookstream)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("Manifest-Version: 1.0");
            sb.Append("\r\n");
            sb.Append("MicroEdition-Configuration: CLDC-1.0");
            sb.Append("\r\n");
            sb.Append("MIDlet-Version: 1.06");
            sb.Append("\r\n");
            sb.Append("Created-By: 1.4.2 (Sun Microsystems Inc.)");
            sb.Append("\r\n");
            sb.AppendFormat("MIDlet-Vendor: {0}",textBox3.Text);
            sb.Append("\r\n");
            sb.Append("MicroEdition-Profile: MIDP-1.0");
            sb.Append("\r\n");
            sb.AppendFormat("MIDlet-1: {0}, /0.png, Txdzs", textBox5.Text);
            sb.Append("\r\n");
            sb.AppendFormat("MIDlet-Name: {0}",textBox5.Text);
            byte[] buffer = Encoding.UTF8.GetBytes(sb.ToString());
            ZipEntry entry = new ZipEntry("META-INF/MANIFEST.MF");
            entry.Size = buffer.Length;
            bookstream.PutNextEntry(entry);
            bookstream.Write(buffer, 0, buffer.Length);
            bookstream.Flush();
            bookstream.CloseEntry();
        }
    }
}
